package com.woniuxy.microsysserver.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.bean.MemberEntity;
import com.woniuxy.bean.ResultBean;
import com.woniuxy.microsysserver.entity.BankCardEntity;
import com.woniuxy.microsysserver.entity.CouponEntity;
import com.woniuxy.microsysserver.entity.MyPage;
import com.woniuxy.microsysserver.service.BankcardService;
import com.woniuxy.microsysserver.service.CouponService;
import com.woniuxy.microsysserver.service.MemberService;
import com.woniuxy.microsysserver.uitl.Constant;
import com.woniuxy.microsysserver.uitl.MD5;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.sql.SQLOutput;
import java.util.List;

/**
 * Auther: wuson  <br/>
 * Date: 2021/4/15:20:04  <br/>
 * Description:
 */
@RefreshScope
@Api("银行卡管理")
@Slf4j
@RequestMapping("/sys/bankcard")
@RestController
public class BankcardController {
    @Autowired(required = false)
    private BankcardService bankcardService;
    @Autowired(required = false)
    private MemberService memberService;

    @ApiOperation("新增用户的银行卡")
    @PostMapping(value = "/addOne", produces = MediaType.APPLICATION_JSON_VALUE)
    public ResultBean addOne (@RequestBody BankCardEntity bankCardEntity) {
        int state = bankcardService.addOne(bankCardEntity);
        return ResultBean.builder().code(200).data(Constant.SUCCESSFUL).build();
    }

    @ApiOperation("给用户绑定银行卡")
    @PostMapping(value = "/binding", produces = MediaType.APPLICATION_JSON_VALUE)
    public ResultBean binding (@RequestBody BankCardEntity currentBankCardEntity) {
        BankCardEntity bankCardEntity = bankcardService.selectBindingByNumber(currentBankCardEntity.getNumber());
        if (bankCardEntity == null ||
                ! (bankCardEntity.getType().equals("广发银行") ||
                        bankCardEntity.getType().equals("交通银行") ||
                        bankCardEntity.getType().equals("农业银行") ||
                        bankCardEntity.getType().equals("中信银行") ||
                        bankCardEntity.getType().equals("建设银行") ||
                        bankCardEntity.getType().equals("工商银行"))
        ) {
            return ResultBean.builder().code(601).data(Constant.BANKCARD_WRONG).build();
        }
        if (! currentBankCardEntity.getMemberUsername().equals(bankCardEntity.getMemberUsername())) {
            return ResultBean.builder().code(600).data(Constant.WRONG).build();
        }
        int state = bankcardService.binding(bankCardEntity);
        return ResultBean.builder().code(200).data(Constant.SUCCESSFUL).build();
    }

    @ApiOperation("删除银行卡")
    @PostMapping(value = "/deleteOne", produces = MediaType.APPLICATION_JSON_VALUE)
    public ResultBean deleteOne (@RequestBody BankCardEntity bankCardEntity) {
        int state = bankcardService.deleteOne(bankCardEntity.getId());
        return ResultBean.builder().code(200).data(Constant.SUCCESSFUL).build();
    }

    //    @ApiOperation("修改银行卡")
//    @PostMapping(value = "/updateOne",produces = MediaType.APPLICATION_JSON_VALUE)
//    public String updateOne(@RequestBody CouponEntity couponEntity){
//        int state = couponConService.updateOne(couponEntity);
//        return JSONObject.toJSONString(ResultBean.builder().code(200).data(Constant.SUCCESSFUL).build());
//    }
    @ApiOperation("分页查询个人银行卡")
    @PostMapping(value = "/selectAll", produces = MediaType.APPLICATION_JSON_VALUE)
    public ResultBean selectAll (@RequestBody MyPage myPage) {
        //开始分页
        PageHelper.startPage(myPage.getPageNum(), myPage.getPageSize());
        //查询结果
        List<BankCardEntity> bankCardEntities = bankcardService.selectAll(myPage.getUsername());
        PageInfo<BankCardEntity> pageInfo = new PageInfo<>(bankCardEntities);
        return ResultBean.builder().code(200).data(pageInfo).build();
    }

    @ApiOperation("银行卡充值余额")
    @PostMapping(value = "/recharge", produces = MediaType.APPLICATION_JSON_VALUE)
    public ResultBean recharge (@RequestBody BankCardEntity currentBankCardEntity) {
        //根据卡号查询结果
        BankCardEntity bankCardEntity = bankcardService.selectByid(currentBankCardEntity.getId());
        if (! bankCardEntity.getPassword().equals(currentBankCardEntity.getPassword())) {
            return ResultBean.builder().code(607).msg(Constant.WRONG_PASSWORD).build();
        }
        if (bankCardEntity.getBalance() < currentBankCardEntity.getRechargeMoney()) {
            return ResultBean.builder().code(600).msg(Constant.INSUFFICIENT_BALANCE).build();
        }
        //扣除银行卡对应余额
        int state = bankcardService.recharge(currentBankCardEntity);
        //增加本人余额
        MemberEntity memberEntity = memberService.selectByAccount(currentBankCardEntity.getMemberUsername());
        int state1 = memberService.updateByName(MemberEntity.builder().username(currentBankCardEntity.getMemberUsername()).balance(memberEntity.getBalance() + currentBankCardEntity.getRechargeMoney()).build());
        return ResultBean.builder().code(200).msg(Constant.SUCCESSFUL).build();
    }

    @ApiOperation("余额提现到银行卡")
    @PostMapping(value = "/withdrawal", produces = MediaType.APPLICATION_JSON_VALUE)
    public ResultBean withdrawal (@RequestBody BankCardEntity currentBankCardEntity) {

        //增加本人余额
        MemberEntity memberEntity = memberService.selectByAccount(currentBankCardEntity.getMemberUsername());
        if (memberEntity.getPayPassword() == null) {
            return ResultBean.builder().code(612).msg(Constant.NO_REAL_NAME).build();
        }
        //密码错误
        if (! memberEntity.getPayPassword().equals(MD5.encrypt(currentBankCardEntity.getPassword()))) {
            return ResultBean.builder().code(607).msg(Constant.WRONG_PASSWORD).build();
        }
        //余额不足
        if (memberEntity.getBalance() < currentBankCardEntity.getRechargeMoney()) {
            return ResultBean.builder().code(600).msg(Constant.INSUFFICIENT_BALANCE).build();
        }
        int state1 = memberService.updateByName(MemberEntity.builder().username(currentBankCardEntity.getMemberUsername()).balance(memberEntity.getBalance() - currentBankCardEntity.getRechargeMoney()).build());
        //扣除银行卡对应余额
        currentBankCardEntity.setRechargeMoney(- currentBankCardEntity.getRechargeMoney());
        int state = bankcardService.recharge(currentBankCardEntity);
        return ResultBean.builder().code(200).msg(Constant.SUCCESSFUL).build();
    }
}
